"""
数据采集模块模型
定义原始数据存储和采集任务相关的模型
"""

from django.db import models
from django.contrib.auth.models import User
from apps.common.models import BaseModel, TaskStatus, DataStatus


class Platform(BaseModel):
    """
    数据源平台模型
    记录各个公共资源交易平台的信息
    """
    name = models.CharField(
        max_length=100,
        verbose_name="平台名称",
        help_text="公共资源交易平台名称"
    )
    base_url = models.URLField(
        verbose_name="基础URL",
        help_text="平台的基础访问地址"
    )
    description = models.TextField(
        blank=True,
        verbose_name="平台描述",
        help_text="平台的详细描述信息"
    )
    is_enabled = models.BooleanField(
        default=True,
        verbose_name="是否启用",
        help_text="是否启用该平台的数据采集"
    )
    crawl_config = models.JSONField(
        default=dict,
        verbose_name="采集配置",
        help_text="平台特定的采集配置参数"
    )

    class Meta:
        verbose_name = "数据源平台"
        verbose_name_plural = "数据源平台"
        db_table = "crawler_platform"

    def __str__(self):
        return self.name


class CrawlTask(BaseModel):
    """
    采集任务模型
    记录数据采集任务的执行情况
    """
    platform = models.ForeignKey(
        Platform,
        on_delete=models.CASCADE,
        verbose_name="目标平台",
        help_text="要采集数据的平台"
    )
    task_name = models.CharField(
        max_length=200,
        verbose_name="任务名称",
        help_text="采集任务的名称"
    )
    status = models.CharField(
        max_length=20,
        choices=TaskStatus.choices,
        default=TaskStatus.PENDING,
        verbose_name="任务状态",
        help_text="当前任务的执行状态"
    )
    start_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name="开始时间",
        help_text="任务开始执行的时间"
    )
    end_time = models.DateTimeField(
        null=True,
        blank=True,
        verbose_name="结束时间",
        help_text="任务结束的时间"
    )
    total_pages = models.IntegerField(
        default=0,
        verbose_name="总页数",
        help_text="需要采集的总页数"
    )
    processed_pages = models.IntegerField(
        default=0,
        verbose_name="已处理页数",
        help_text="已经处理完成的页数"
    )
    success_count = models.IntegerField(
        default=0,
        verbose_name="成功数量",
        help_text="成功采集的数据条数"
    )
    error_count = models.IntegerField(
        default=0,
        verbose_name="错误数量",
        help_text="采集失败的数据条数"
    )
    error_message = models.TextField(
        blank=True,
        verbose_name="错误信息",
        help_text="任务执行过程中的错误信息"
    )
    config = models.JSONField(
        default=dict,
        verbose_name="任务配置",
        help_text="任务的具体配置参数"
    )
    created_by = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name="创建者",
        help_text="创建该任务的用户"
    )

    class Meta:
        verbose_name = "采集任务"
        verbose_name_plural = "采集任务"
        db_table = "crawler_task"
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.platform.name} - {self.task_name}"

    @property
    def progress_percentage(self):
        """计算任务进度百分比"""
        if self.total_pages == 0:
            return 0
        return round((self.processed_pages / self.total_pages) * 100, 2)


class RawData(BaseModel):
    """
    原始数据模型
    存储从各平台采集到的原始数据
    """
    platform = models.ForeignKey(
        Platform,
        on_delete=models.CASCADE,
        verbose_name="数据源平台",
        help_text="数据来源平台"
    )
    crawl_task = models.ForeignKey(
        CrawlTask,
        on_delete=models.CASCADE,
        null=True,
        blank=True,
        verbose_name="采集任务",
        help_text="关联的采集任务"
    )
    source_url = models.URLField(
        max_length=500,
        verbose_name="源URL",
        help_text="数据的原始URL地址"
    )
    title = models.CharField(
        max_length=500,
        blank=True,
        verbose_name="标题",
        help_text="数据的标题信息"
    )
    raw_content = models.JSONField(
        verbose_name="原始内容",
        help_text="采集到的原始数据内容"
    )
    content_hash = models.CharField(
        max_length=64,
        db_index=True,
        verbose_name="内容哈希",
        help_text="内容的MD5哈希值，用于去重"
    )
    fetch_time = models.DateTimeField(
        auto_now_add=True,
        verbose_name="采集时间",
        help_text="数据采集的时间"
    )
    status = models.CharField(
        max_length=20,
        choices=DataStatus.choices,
        default=DataStatus.RAW,
        verbose_name="数据状态",
        help_text="数据的处理状态"
    )
    file_urls = models.JSONField(
        default=list,
        verbose_name="文件URL列表",
        help_text="相关文件的URL地址列表"
    )
    metadata = models.JSONField(
        default=dict,
        verbose_name="元数据",
        help_text="额外的元数据信息"
    )

    class Meta:
        verbose_name = "原始数据"
        verbose_name_plural = "原始数据"
        db_table = "crawler_raw_data"
        ordering = ['-fetch_time']
        indexes = [
            models.Index(fields=['platform', 'status']),
            models.Index(fields=['content_hash']),
            models.Index(fields=['fetch_time']),
        ]

    def __str__(self):
        return f"{self.platform.name} - {self.title[:50]}"
